package com.collect.zambia.zhiAdater.zhiTelemarketing;

import android.text.TextUtils;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.collect.zambia.MyApplication;
import com.collect.zambia.R;
import com.collect.zambia.zhiBean.zhiReceive.ZHIRTelemarketingFollowEntity;
import com.collect.zambia.zhiHelper.ZHIDsHelper;
import com.collect.zambia.zhiHelper.ZHISysShareHelper;
import com.collect.zambia.zhiScene.zhiTelemarketing.zhiLogic.ZHITelemarketingFollowHelper;
import com.collect.zambia.zhiScene.zhiWidget.zhiPopupWindow.ZHIPopupTelemaketingFollow;
import com.collect.zambia.zhiUtils.ToastUtil;

public class ZHITelemarketingFollowAdjuster extends BaseQuickAdapter<ZHIRTelemarketingFollowEntity, BaseViewHolder> {

    public ZHITelemarketingFollowAdjuster() {
        super(R.layout.item_telemarketing_follow_list, null);
    }

    private ZHITelemarketingItemClickListener mZHITelemarketingItemClickListener;

    @Override
    protected void convert(BaseViewHolder helper, ZHIRTelemarketingFollowEntity item) {
        try {
            helper.setText(R.id.tv_assign_date, item.getAssignDate());
            helper.setText(R.id.tv_product_name, item.getTargetProduct());
            helper.setText(R.id.tv_client_type, item.getClientType());
            helper.setText(R.id.tv_client_name, item.getMobileEncryptFormatter());
            helper.setText(R.id.tv_follow_time, item.getLastFollowTime());
            helper.setText(R.id.tv_follow_state, item.getFollowStatus());
//            helper.setText(R.id.tv_client_name, item.getFullName());

//            helper.setText(R.id.tv_whatsapp_count, item.getFollowNum());
//            helper.setText(R.id.tv_remark, item.getRemarkSpanFormat());
//            helper.setText(R.id.tv_mobile, item.getMobileEncryptFormatter());
//FOLLOWED,FOCUS_ON
            LinearLayout ll_follow_state = helper.getView(R.id.ll_follow_state);
            ll_follow_state.setOnClickListener(v -> {
                //显示下拉框
                ZHIPopupTelemaketingFollow.showPopWindow(mContext, ll_follow_state, followState -> {
                    if (!TextUtils.isEmpty(followState)) {
                        //判断能不能跟进
//                        boolean canFollowed = ZHITelemarketingFollowHelper.getInstance().checkDidFollowedPhoneCall(item.getTargetProduct(), item.getMobile());
//                        canFollowed = (canFollowed || (item.getWhatsappFollowNumInt() > 0));
                        boolean canFollowed = checkCallResult(item.getMobile());
                        if (canFollowed) {
                            if (mZHITelemarketingItemClickListener != null) {
                                ZHITelemarketingFollowHelper.getInstance().removeFollowCache(item.getTargetProduct(), item.getMobile());
                                mZHITelemarketingItemClickListener.OnItemFollowStateChangedListener(item, helper.getLayoutPosition(), followState);
                            }
                        } else {
                            ToastUtil.s("Please follow up the order before changing the status");
                        }
                    }
                });
            });

            LinearLayout ll_make_call = helper.getView(R.id.ll_make_call);
            ll_make_call.setOnClickListener(v -> {
                //显示下拉框
                try {
                    //跟进开始
                    ZHITelemarketingFollowHelper.getInstance().setFollowPhoneCallStart(item.getTargetProduct(), item.getMobile());
                    ZHITelemarketingFollowHelper.FOLLOW_TYPE = ZHITelemarketingFollowHelper.FOLLOW_TYPE_PHONE_CALL;
                    ZHISysShareHelper.callPhone(MyApplication.getAppContext(), item.getMobile());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

            LinearLayout ll_send_whatsapp = helper.getView(R.id.ll_send_whatsapp);
            ll_send_whatsapp.setOnClickListener(v -> {
                //显示下拉框
                if (ZHISysShareHelper.isWhatsappInstall(mContext)) {
                    if (mZHITelemarketingItemClickListener != null) {
                        mZHITelemarketingItemClickListener.OnWhatsappFollowClick(item);
                    }

                    // 交给上层处理
//                    ZHITelemarketingFollowHelper.getInstance().setFollowWhatsappStart(item.getTargetProduct(), item.getMobile());
//                    ZHITelemarketingFollowHelper.FOLLOW_TYPE = ZHITelemarketingFollowHelper.FOLLOW_TYPE_WHATSAPP;
//                    ZHISysShareHelper.sendMsgWithWhatsApp(mContext,  ZHIProjectConstants.PRE_AREA_FLAG + item.getMobile(), item.getWhatsAppContent());
                } else {
                    ToastUtil.s("You haven't installed whatsAPP");
                }
            });

//            helper.getView(R.id.tv_follow).setOnClickListener(view -> {
//                if (mZHITelemarketingItemClickListener != null) {
//                    mZHITelemarketingItemClickListener.OnItemClick(item);
//                }
//            });

//            helper.getView(R.id.tv_mobile).setOnClickListener(v -> ZHISysShareHelper.callPhone(MyApplication.getAppContext(), item.getMobile()));
//            helper.getView(R.id.iv_call_phone).setOnClickListener(v -> ZHISysShareHelper.callPhone(MyApplication.getAppContext(), item.getMobile()));
//            helper.getView(R.id.iv_whatsapp).setOnClickListener(v -> {
//                if (!TextUtils.isEmpty(item.getWhatsAppContent())) {
//                    if (mZHITelemarketingItemClickListener != null) {
//                        mZHITelemarketingItemClickListener.OnWhatsappFollowClick(item);
//                    }
//                    ZHISysShareHelper.sendMsgWithWhatsApp(mContext,  ZHIProjectConstants.PRE_AREA_FLAG + item.getMobile(), item.getWhatsAppContent());
//                } else {
//                    ToastUtil.s("No content can be send.");
//                }
//            });

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private boolean checkCallResult(String mobile) {
        int result = ZHIDsHelper.getInstance().checkCallRecord(mobile);
        if (ZHIDsHelper.RESULT_CALL_SUCCESS == result) {
            return true;
        }

        ToastUtil.s("Is this really the case? Please take the job seriously-" + result);
        return false;
    }

    public void setZHITelemarketingItemClickListener(ZHITelemarketingItemClickListener zhiTelemarketingItemClickListener) {
        this.mZHITelemarketingItemClickListener = zhiTelemarketingItemClickListener;
    }
}
